<template>
  <el-dialog
    :title="!dataForm.apprId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户唯一ID" prop="patientId">
      <el-input v-model="dataForm.patientId" placeholder="用户唯一ID"></el-input>
    </el-form-item>
    <el-form-item label="订单外键" prop="orderId">
      <el-input v-model="dataForm.orderId" placeholder="订单外键"></el-input>
    </el-form-item>
    <el-form-item label="评价星级" prop="apprRank">
      <el-input v-model="dataForm.apprRank" placeholder="评价星级"></el-input>
    </el-form-item>
    <el-form-item label="商品名称" prop="productName">
      <el-input v-model="dataForm.productName" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="评价内容" prop="apprContent">
      <el-input v-model="dataForm.apprContent" placeholder="评价内容"></el-input>
    </el-form-item>
    <el-form-item label="评价图片" prop="apprImg">
      <el-input v-model="dataForm.apprImg" placeholder="评价图片"></el-input>
    </el-form-item>
    <el-form-item label="评价时间" prop="apprDate">
      <el-input v-model="dataForm.apprDate" placeholder="评价时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          apprId: 0,
          patientId: '',
          orderId: '',
          apprRank: '',
          productName: '',
          apprContent: '',
          apprImg: '',
          apprDate: ''
        },
        dataRule: {
          patientId: [
            { required: true, message: '用户唯一ID不能为空', trigger: 'blur' }
          ],
          orderId: [
            { required: true, message: '订单外键不能为空', trigger: 'blur' }
          ],
          apprRank: [
            { required: true, message: '评价星级不能为空', trigger: 'blur' }
          ],
          productName: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          apprContent: [
            { required: true, message: '评价内容不能为空', trigger: 'blur' }
          ],
          apprImg: [
            { required: true, message: '评价图片不能为空', trigger: 'blur' }
          ],
          apprDate: [
            { required: true, message: '评价时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.apprId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.apprId) {
            this.$http({
              url: this.$http.adornUrl(`/product/shopappraise/info/${this.dataForm.apprId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.patientId = data.shopAppraise.patientId
                this.dataForm.orderId = data.shopAppraise.orderId
                this.dataForm.apprRank = data.shopAppraise.apprRank
                this.dataForm.productName = data.shopAppraise.productName
                this.dataForm.apprContent = data.shopAppraise.apprContent
                this.dataForm.apprImg = data.shopAppraise.apprImg
                this.dataForm.apprDate = data.shopAppraise.apprDate
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/product/shopappraise/${!this.dataForm.apprId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'apprId': this.dataForm.apprId || undefined,
                'patientId': this.dataForm.patientId,
                'orderId': this.dataForm.orderId,
                'apprRank': this.dataForm.apprRank,
                'productName': this.dataForm.productName,
                'apprContent': this.dataForm.apprContent,
                'apprImg': this.dataForm.apprImg,
                'apprDate': this.dataForm.apprDate
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
